package com.youbar.client.basic;

import com.youbar.client.enums.ResultCode;

import java.io.Serializable;

public class ApiResult<T> implements Serializable {

    private int code;
    private String message;
    private T data;
    private boolean success;

    public ApiResult() {
    }

    @Deprecated
    public ApiResult(int code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }
    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public ApiResult(BizResult result) {
        this.code = result.getCode();
        this.message = result.getMessage();
        this.success = result.isSuccess();
        this.data = (T)result.getData();
    }

    public ApiResult(ResultCode code) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.success = code.isSuccess();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static ApiResult me(){
        return new ApiResult();
    }

    /**
     * 访问成功，无返回数据
     * @return
     */
    public static ApiResult success() {
        return new ApiResult(ResultCode.SUCCESS);
    }

    /**
     * 访问成功
     * @param o 返回数据
     * @return
     */
    public static ApiResult success(Object o){
        ApiResult result =  new ApiResult(ResultCode.SUCCESS);
        result.setData(o);
        return result;
    }

    /**
     * 访问失败
     * @param message 提示
     * @return
     */
    public static ApiResult error(String message){
        ApiResult result =  new ApiResult(ResultCode.ERROR);
        result.setMessage(message);
        return result;
    }

    /**
     * 访问失败
     * @param message 提示
     * @param t 对象
     * @return
     */
    public static ApiResult error(String message, Object t){
        ApiResult result =  new ApiResult(ResultCode.ERROR);
        result.setMessage(message);
        result.setData(t);
        return result;
    }

    /**
     * 访问失败
     * @param message 提示
     * @return
     */
    public static ApiResult paramError(String message){
        ApiResult result =  new ApiResult(ResultCode.UN_VAILD_PARA);
        result.setMessage(message);
        return result;
    }

    /**
     * 参数错误
     * @return
     */
    public static ApiResult paramError(){
        ApiResult result =  new ApiResult(ResultCode.UN_VAILD_PARA);
        return result;
    }
}
